//
//  ReViewController.m
//  YiLiao
//
//  Created by mac on 16/7/1.
//  Copyright © 2016年 Noah. All rights reserved.
//

#import "ReViewController.h"
#import "LoginViewController.h"
#import <AFHTTPSessionManager.h>
#import "Util.h"
#import "NetTool.h"
@interface ReViewController ()

@end

@implementation ReViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.title = @"登录";
}



//注册界面
- (IBAction)qrReBut:(id)sender {
    
    if (![self checkISNull]) {
        return;
    }
    
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    if (manager) {
        NSDictionary *bodyDic=@{@"command":@"ST_R",@"name":self.YTextField.text,@"psw":self.MTextField.text,@"nickname":self.NTextField.text,@"email":self.YXTextField.text};
        [manager POST:@"http://localhost:8080/st/s" parameters:bodyDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            [NetTool hidelndicator];
             NSLog(@"------%@",[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
            if (responseObject)
            {
                NSDictionary *resultDic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                
                
                if ([resultDic[@"result"] isEqualToString:@"1"])
                {
                  [Util showAlertWithMessager:@"恭喜!注册成功!" confirmBlock:^
                    {
                        [self dismissViewControllerAnimated:YES completion:nil];
                  }];
                }
                else{
                    [Util showAlertWithMessager:@"该用户名已被注册!" confirmBlock:^
                    {
                        [_YTextField becomeFirstResponder ];
                    }];
                }
            }
            
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
        {
            
            [NetTool hidelndicator];
            [Util showAlertWithMessager:@"请求数据失败,请稍后重试" confirmBlock:nil];
        }];
        
    }
    
    
//    
//    if (self.MTextField.text.length<3&&self.YTextField.text.length<2&&[self.MTextField.text isEqualToString:self.QTextField.text]) {
//        NSLog(@"用户名或密码过短，请重试");
//    }else{
//        AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
//        manager .responseSerializer.acceptableContentTypes=[manager.responseSerializer.acceptableContentTypes setByAddingObjectsFromSet:[NSSet setWithObjects:@"text/html", nil]];
//        NSDictionary *bodyDic=@{@"command":@"ST_R",@"name":self.YTextField.text,@"psw":self.MTextField.text,@"nickname":self.NTextField.text,@"email":self.YXTextField.text};
//        [manager POST:@"http://localhost:8080/st/s" parameters:bodyDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//            NSDictionary *infoDic=(NSDictionary *)responseObject;
//            NSLog(@"----%@",infoDic);
//            NSString *str=[infoDic objectForKey:@"result"];
//            if ([str isEqualToString:@"1"]) {
//                NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
//                [user setObject:self.YTextField.text forKey:@"name"];
//                [user setObject:self.MTextField.text forKey:@"psw"];
//            }
//        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//            NSLog(@"+++++%@",error);
//        }];
//    }
//    
//    
//    
//    
//    LoginViewController *LVC = [[LoginViewController alloc] init];
//    
//    [self dismissViewControllerAnimated:LVC completion:nil];
}

/*
 @property (weak, nonatomic) IBOutlet UITextField *YTextField;
 @property (weak, nonatomic) IBOutlet UITextField *MTextField;
 @property (weak, nonatomic) IBOutlet UITextField *QTextField;
 @property (weak, nonatomic) IBOutlet UITextField *NTextField;
 @property (weak, nonatomic) IBOutlet UITextField *YXTextField;
 */
-(BOOL)checkISNull{
    if ([_YTextField.text isEqualToString:@""]) {
        [Util showAlertWithMessager: @"用户名不能为空!" confirmBlock:^{
            [_YTextField becomeFirstResponder];
        }];
        return  NO;
    }
    if ([_MTextField.text isEqualToString:@""]) {
        [Util showAlertWithMessager: @"密码不能为空!" confirmBlock:^{
            [_MTextField becomeFirstResponder];
        }];
        return  NO;
    }
    if ([_MTextField.text isEqualToString:_QTextField.text]) {
        [Util showAlertWithMessager: @"两次密码不一致,请确认后再重试!" confirmBlock:^{
            [_QTextField becomeFirstResponder];
        }];
        return  NO;
    }
    return YES;
}






























































- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
